<template>
  <div class="category-management">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>菜品分类管理</span>
          <el-button type="primary" @click="handleAdd">新增分类</el-button>
        </div>
      </template>
      
      <!-- 分类列表 -->
      <el-table :data="categoryList" stripe style="width: 100%" v-loading="loading">
        <el-table-column prop="id" label="分类ID" width="80" />
        <el-table-column prop="name" label="分类名称" />
        <el-table-column prop="description" label="描述" />
        <el-table-column prop="sortOrder" label="排序" width="100" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'">
              {{ scope.row.status === 1 ? '启用' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180" />
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button 
              size="small" 
              :type="scope.row.status === 1 ? 'danger' : 'success'"
              @click="handleToggleStatus(scope.row)"
            >
              {{ scope.row.status === 1 ? '禁用' : '启用' }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <!-- 分页 -->
      <el-pagination
        v-model:current-page="pagination.currentPage"
        v-model:page-size="pagination.pageSize"
        :page-sizes="[10, 20, 50]"
        :total="pagination.total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        style="margin-top: 20px; justify-content: flex-end;"
      />
    </el-card>
    
    <!-- 分类编辑对话框 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="500px">
      <el-form :model="currentCategory" label-width="80px">
        <el-form-item label="分类名称">
          <el-input v-model="currentCategory.name" />
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="currentCategory.description" type="textarea" />
        </el-form-item>
        <el-form-item label="排序">
          <el-input-number v-model="currentCategory.sortOrder" :min="0" />
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="currentCategory.status"
            :active-value="1"
            :inactive-value="0"
            active-text="启用"
            inactive-text="禁用"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="saveCategory">保存</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 模拟分类数据
const mockCategoryList = [
  { id: 1, name: '川菜', description: '四川风味菜品', sortOrder: 1, status: 1, createTime: '2025-01-01 12:00:00' },
  { id: 2, name: '粤菜', description: '广东风味菜品', sortOrder: 2, status: 1, createTime: '2025-01-01 12:00:00' },
  { id: 3, name: '湘菜', description: '湖南风味菜品', sortOrder: 3, status: 1, createTime: '2025-01-01 12:00:00' },
  { id: 4, name: '鲁菜', description: '山东风味菜品', sortOrder: 4, status: 0, createTime: '2025-01-01 12:00:00' }
]

// 分类列表数据
const categoryList = ref([])
const loading = ref(false)

// 分页信息
const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0
})

// 对话框相关
const dialogVisible = ref(false)
const dialogTitle = ref('')
const isEdit = ref(false)

// 当前编辑的分类
const currentCategory = reactive({
  id: 0,
  name: '',
  description: '',
  sortOrder: 0,
  status: 1
})

// 获取分类列表
const fetchCategoryList = async () => {
  loading.value = true
  try {
    // 模拟API调用
    await new Promise(resolve => setTimeout(resolve, 500))
    categoryList.value = mockCategoryList
    pagination.total = mockCategoryList.length
  } catch (err) {
    ElMessage.error('获取分类列表失败')
  } finally {
    loading.value = false
  }
}

// 处理新增分类
const handleAdd = () => {
  dialogTitle.value = '新增分类'
  isEdit.value = false
  // 重置表单
  Object.assign(currentCategory, {
    id: 0,
    name: '',
    description: '',
    sortOrder: 0,
    status: 1
  })
  dialogVisible.value = true
}

// 处理编辑分类
const handleEdit = (category) => {
  dialogTitle.value = '编辑分类'
  isEdit.value = true
  // 填充表单数据
  Object.assign(currentCategory, category)
  dialogVisible.value = true
}

// 保存分类
const saveCategory = () => {
  ElMessage.success(`${isEdit.value ? '编辑' : '新增'}分类成功`)
  dialogVisible.value = false
  fetchCategoryList()
}

// 处理切换分类状态
const handleToggleStatus = (category) => {
  const newStatus = category.status === 1 ? 0 : 1
  const action = newStatus === 1 ? '启用' : '禁用'
  
  ElMessageBox.confirm(`确定要${action}分类"${category.name}"吗？`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    ElMessage.success(`${action}分类成功`)
    category.status = newStatus
  }).catch(() => {
    ElMessage.info('已取消操作')
  })
}

// 分页相关处理
const handleSizeChange = (val) => {
  pagination.pageSize = val
  fetchCategoryList()
}

const handleCurrentChange = (val) => {
  pagination.currentPage = val
  fetchCategoryList()
}

// 组件挂载时获取数据
onMounted(() => {
  fetchCategoryList()
})
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.dialog-footer {
  text-align: right;
}
</style>